package com.xyd.Servlet;

import com.xyd.Biz.CommodityBiz;
import com.xyd.Biz.FatherOrderBiz;
import com.xyd.Biz.Impl.CommodityBizImpl;
import com.xyd.Biz.Impl.FatherOrderBizImpl;
import com.xyd.Biz.Impl.OrderFormBizImpl;
import com.xyd.Biz.Impl.ProductCarBizImpl;
import com.xyd.Biz.OrderFormBiz;
import com.xyd.Biz.ProductCarBiz;
import com.xyd.Entity.*;
import com.xyd.Util.SnowFlakeIdWorker;
import com.xyd.Util.SqlSessionFactoryUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: OrderServlet
 * @Description:
 * @Author 余全盛
 * @Date: 2021/10/15 16:58
 */
@WebServlet("/OrderServlet")
public class OrderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        ProductCarBiz productCarBiz=new ProductCarBizImpl();
        FatherOrderBiz fatherOrderBiz = new FatherOrderBizImpl();
        OrderFormBiz orderFormBiz = new OrderFormBizImpl();
        //获取购物车中每个商品的数量
        String[] strnums = req.getParameterValues("num");
        int length = strnums.length;
        int[] nums = new int[length];
        for (int i = 0; i < length; i++) {
            nums[i] = Integer.parseInt(strnums[i] == null || "".equals(strnums[i]) ? "0" : strnums[i]);

        }
        //获取购物车中每个商品的id
        String[] strproductids = req.getParameterValues("productid");

        int[] productids = new int[length];
        for (int i = 0; i < length; i++) {
            productids[i] = Integer.parseInt(strproductids[i] == null || "".equals(strproductids[i]) ? "0" : strproductids[i]);

        }
        //获取购物车中每个商品的价格,并计算每个商品的数量*价格,结果保留一位小数
        double indivSum[] = new double[length];
        String[] strprices = req.getParameterValues("price");
        double[] prices = new double[length];
        for (int i = 0; i < length; i++) {
            prices[i] = Double.parseDouble(strprices[i] == null || "".equals(strprices[i]) ? "0.00" : strprices[i]);
            indivSum[i] = prices[i] * nums[i];
            String str = new DecimalFormat("#.00").format(indivSum[i]);
            indivSum[i] = Double.parseDouble(str);
        }
        //添加子订单，计算总价,得到每个子订单下商品的信息
        double sum = 0;
        SnowFlakeIdWorker snowFlakeIdWorker = new SnowFlakeIdWorker(0, 0);
        long fatherOrderID = snowFlakeIdWorker.nextId();
        for (int i = 0; i < length; i++) {
            orderFormBiz.addSonOrder(productids[i], nums[i], prices[i], indivSum[i], fatherOrderID);
            sum += indivSum[i];
        }
       User user=(User) session.getAttribute("user");
        List<ProductCar> productList = productCarBiz.selectAllProductCarByID(user.getUserid());
        List<OrderForm> sonOrderList = orderFormBiz.selectSonOrder(fatherOrderID);
//        生成父订单，跳转订单详情页面
        fatherOrderBiz.addOrder(fatherOrderID, sum, user);
        FatherOrder fatherOrder = fatherOrderBiz.selectFatherOrderByID(fatherOrderID);
        session.setAttribute("productList", productList);
        session.setAttribute("sonOrderList", sonOrderList);
        session.setAttribute("fatherOrder", fatherOrder);
        resp.sendRedirect("/shopping/html/shopping-result.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
